package com.ys.service.kefu.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.ys.config.MyConfig;
import com.ys.dto.AddDto;
import com.ys.entity.kefu.TkfKnow;
import com.ys.mapper.kefu.TkfKnowMapper;
import com.ys.service.kefu.TkfKnowService;
import com.ys.vo.R;

@Service("tkfKnowService")
public class TkfKnowServiceImpl implements TkfKnowService {
	
	@Autowired
	private TkfKnowMapper tkfKnowMapper;
	
	@Override
	@Transactional
	public R add(Map<String, Object> params, Long menuId) throws Exception {
		
		TkfKnow entity = JSON.parseObject(JSON.toJSONString(params), TkfKnow.class);
		
		entity.setStatus(1);
		
		tkfKnowMapper.insert(entity);
		
		return R.ok(AddDto.closeAndReload());
	}
	
	@Override
	@Transactional
	public R edit(Map<String, Object> params) throws Exception {
		TkfKnow entity = JSON.parseObject(JSON.toJSONString(params), TkfKnow.class);
		
		tkfKnowMapper.update(entity);
		return R.ok("操作成功");
	}

	@Override
	public R deleteTrue(Long id) throws Exception {
		if (id == null) {
			return R.error("ID不能为空");
		}
		
		tkfKnowMapper.deleteTrue(id);
		return R.ok();
	}

	@Override
	public String getAutoContentByMsgContent(String content) {
		
		List<TkfKnow> listTkfKnow = new TkfKnow().where("A.Title like '%" + content + "%'").queryList();
		
		if (listTkfKnow == null || listTkfKnow.size() == 0) {
			return "小逸无法识别您的问题，请把问题范围缩小，例如：审批";
		}
		
		String urlPre = MyConfig.getUrlDomain() + "/web/kefu/tkfKnow/toDetail?id=";
		String contentAuto = "";
		int index = 1;
		for (TkfKnow tkfKnow : listTkfKnow) {
			contentAuto = contentAuto + index + "、" + tkfKnow.getTitle() + "：a(" + (urlPre + tkfKnow.getId()) + ")[>>]" + "\n";
			index++;
		}
		
		return contentAuto;
	}
}
